//
//  CustomizeProgressView.m
//  Learn
//
//  Created by zyl on 2024/4/2.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "CustomizeProgressView.h"

@interface CustomizeProgressView ()

@property(nonatomic ,strong)UIView *potView;

@property(nonatomic ,strong)UIView *slideView;//滑道 默认高度22pt

@property(nonatomic ,strong)UIView *lineGreenView;//滑道 默认高度22pt

@property(nonatomic ,strong)NSArray *titleArray;

@property(nonatomic ,strong)NSMutableArray *lineViewArray;

@end
@implementation CustomizeProgressView

- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titles Progress:(double)progress
{
    self = [super initWithFrame:frame];
    self.titleArray = titles;
    self.lineViewArray = @[].mutableCopy;
    [self setUI];
    self.lineGreenView.width = progress*self.width;
    self.potView.centerX = progress*self.width;
    [self changeLineColor];
    return self;
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.slideView];
    if(p.x < 0||p.x > self.slideView.width)
    {
        return;
    }
    if ((p.x - self.lineGreenView.width) != 0) {
        self.lineGreenView.width =  self.lineGreenView.width + p.x - self.lineGreenView.width;
        self.potView.centerX = self.lineGreenView.width;
        [self changeLineColor];
        if (self.block) {
            self.block(self.potView.centerX/self.slideView.width);
        }
    }
}
- (void)changeLineColor
{
    for (int i = 0; i < self.lineViewArray.count; i++) {
        UIView *line = self.lineViewArray[i];
        if (line.x <  self.lineGreenView.width ) {
            line.backgroundColor = rgba(12, 183, 108, 1);
        }else{
            line.backgroundColor = rgba(229, 229, 229, 1);
        }
    }
}
- (void)setUI
{
    [self addSubview:self.slideView];
    [self.slideView addSubview:self.lineGreenView];
    double space = (self.slideView.width - self.titleArray.count*2.00)/(self.titleArray.count - 1);
    for (int i = 0; i < self.titleArray.count; i++) {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0 + (space + 2)*i, self.lineGreenView.y - 3, 2, 3)];
        line.backgroundColor = rgba(229, 229, 229, 1);
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, self.slideView.bottom, 100, 12)];
        title.textColor = rgba(51, 51, 51, 1);
        title.font = RFontWithSize(12);
        title.text = self.titleArray[i];
        [title sizeToFit];
        title.height = 12;
        title.centerX = line.centerX;
        [self.lineViewArray addObject:line];
        if (i==0) {
            title.x = 0;
            
        }
        
        if (i==self.titleArray.count - 1) {
            title.x = self.slideView.width - title.width;
        }
        [self addSubview:title];
        [self.slideView addSubview:line];
        self.height = title.bottom;
    }
    [self.slideView addSubview:self.potView];
}
- (UIView *)potView
{
    if (!_potView) {
        _potView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.slideView.height - 22)/2.0, 22, 22)];
        UIView *linePot = [[UIView alloc] initWithFrame:CGRectMake((22 - 9)/2.0, (22 - 9)/2.0, 9, 9)];
        linePot.backgroundColor = rgba(12, 183, 108, 1);
        [_potView addSubview:linePot];
        [linePot setAllCornersWithCornerRadius:4.5];
        _potView.userInteractionEnabled = NO;
        _potView.backgroundColor = lClearColor;
    }
    return _potView;
}
- (UIView *)slideView
{
    if (!_slideView) {
        _slideView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.height - 22)/2.0, self.width, 22)];
        UIView *lineGray = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.width, 2)];
        lineGray.backgroundColor = rgba(229, 229, 229, 1);
        [_slideView addSubview:lineGray];
        lineGray.userInteractionEnabled = NO;
        _slideView.backgroundColor = lClearColor;
    }
    return _slideView;
}
- (UIView *)lineGreenView{
    if (!_lineGreenView) {
     
        _lineGreenView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.width, 2)];
        _lineGreenView.backgroundColor = rgba(12, 183, 108, 1);
        _lineGreenView.userInteractionEnabled = NO;
    }
    return _lineGreenView;
}
@end
